package exam.axhu.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.entity.Exam;
import exam.axhu.entity.Student;
import exam.axhu.entity.Teacher;
import exam.axhu.service.CourseService;
import exam.axhu.service.ExamService;
import exam.axhu.service.StudentService;
import exam.axhu.service.TeacherService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class CourseAction extends ActionSupport{
//defaultClass: teacherLogin
	@Resource TeacherService teacherService;
	@Resource StudentService studentService;
	@Resource CourseService courseService;
	@Resource ExamService examService;
	
	private String teacherID = (String)ActionContext.getContext().getSession().get("teacherID");
	private String studentID = (String)ActionContext.getContext().getSession().get("studentID");
	private CourseInfo courseInfo;
	private Course course;
	private String courseID;
	private String searchInfo;

	public String getSearchInfo() {
		return searchInfo;
	}
	public void setSearchInfo(String searchInfo) {
		this.searchInfo = searchInfo;
	}
	public CourseInfo getCourseInfo() {
		return courseInfo;
	}
	public void setCourseInfo(CourseInfo courseInfo) {
		this.courseInfo = courseInfo;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public String getCourseID() {
		return courseID;
	}
	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}
	
	public String AddCourseA(){
		return "AddCourse";
	}
	public String AddCourseB(){
		courseInfo.setTeacherID(teacherID);
		if(courseService.findByCourseID(courseInfo.getCourseID())!=null)
		{	
			addActionMessage("系统检测到该课程号之前已经注册过!请修改课程号");
			return "AddCourseFailed";			
		}
		
		else{ 
			courseService.addCourse(courseInfo);
			addActionMessage("课程添加成功!");
			ActionContext.getContext().put("teacherInfo", teacherService.find(teacherID));
			ActionContext.getContext().put("courses",courseService.findByTeacherID(teacherID));
			return "AddCourseSucceed";
				
		}
		
	}
	
	public String deleteCourseA(){
		ActionContext.getContext().put("deletecourseInfo",courseService.findByCourseID(courseID));
		return "DeleteCourse";
	}
	public String deleteCourseB(){
		
		if(courseInfo!=null)
		{
			courseInfo.setTeacherID(teacherID);
			courseService.deleteCourse(courseInfo.getCourseID());
		}
		ActionContext.getContext().put("courses", courseService.findByTeacherID(teacherID));
		return "DeleteCourseSucceed";
	}

	public String getInCourse(){
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("exam0",examService.listExamsByCourseAndStatus(courseID,0)); //试卷编辑中
		ActionContext.getContext().put("exam2",examService.listExamsByCourseAndStatus(courseID,2)); //试卷审阅中
		ActionContext.getContext().put("exam3",examService.listExamsByCourseAndStatus(courseID,3)); //已批阅试卷
		return "GetInCourse";
	}
	
	public String registedStudents(){
		List<Course> courses = courseService.listStudentsByCourseID(courseID);
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("students",students);
		return "RegistedStudents";
	}
	
	public String deleteRegistedStudentA(){
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(course.getCourseID()));
		ActionContext.getContext().put("deletecourse",courseService.searchStudent(course.getCourseID(),course.getStudentID()));
		return "Delete";
	}
	
	public String deleteRegistedStudentB(){
		if(course.getStudentID()!=null)
		{
			courseService.deleteStudent(course.getCourseID(),course.getStudentID());
			courseID=course.getCourseID();
		}
		List<Course> courses = courseService.listStudentsByCourseID(course.getCourseID());
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(course.getCourseID()));
		ActionContext.getContext().put("needCourseID",course.getCourseID());
		ActionContext.getContext().put("students",students);
		return "DeleteRegistedStudentSucceed";
	}

	public String addRegistedStudentA(){
		ActionContext.getContext().put("students",studentService.list());
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		return "Add";
	}
	
	public String addRegistedStudentB(){
		if(courseService.searchStudent(course.getCourseID(),course.getStudentID())==null)
			courseService.addStudent(course.getCourseID(),course.getStudentID());
		List<Course> courses = courseService.listStudentsByCourseID(course.getCourseID());
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(course.getCourseID()));
		ActionContext.getContext().put("needCourseID",course.getCourseID());
		ActionContext.getContext().put("students",students);
		return "AddRegistedStudentSucceed";
	}
	
	public String searchRegistedStudent(){
		if(searchInfo.length()!=0){
			if(studentService.find(searchInfo)!=null){
				//search by studentID
				ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
				ActionContext.getContext().put("needCourseID",courseID);
				ActionContext.getContext().put("findStudent", studentService.find(searchInfo));
				return "SearchSucceed";
			}
			if(studentService.findByName(searchInfo).size()!=0){
				ActionContext.getContext().put("findStudent", studentService.findByName(searchInfo));
				ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
				ActionContext.getContext().put("needCourseID",courseID);
				return "SearchSucceed";
			}
			ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
			ActionContext.getContext().put("students",studentService.list());
			ActionContext.getContext().put("needCourseID",courseID);
			addActionMessage("没有找到该学生记录！请输入学生姓名或学号~");
			return "SearchFailed";
		}
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("students",studentService.list());
		ActionContext.getContext().put("needCourseID",courseID);
		addActionMessage("请输入学生姓名或学号");
		return 	"SearchFailed";
	}
	
	public String search(){
		if(searchInfo.length()!=0){
			if(studentService.find(searchInfo)!=null){
				//search by studentID
				ActionContext.getContext().put("findStudent", studentService.find(searchInfo));
				return "SearchSucceed";
			}
			if(studentService.findByName(searchInfo).size()!=0){
				ActionContext.getContext().put("findStudent", studentService.findByName(searchInfo));
				return "SearchSucceed";
			}
			ActionContext.getContext().put("students",studentService.list());
			ActionContext.getContext().put("needCourseID",courseID);
			addActionMessage("没有找到该学生记录！请输入学生姓名或学号~");
			return "SearchFailed";
		}
		
		ActionContext.getContext().put("students",studentService.list());
		ActionContext.getContext().put("needCourseID",courseID);
		addActionMessage("请输入学生姓名或学号");
		return 	"SearchFailed";
	}
	
	public String startCourse(){
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("exam1",examService.listExamsByCourseAndStatus(courseID,1)); //开始考试
		ActionContext.getContext().put("exam2",examService.listExamsByCourseAndStatus(courseID,2)); //试卷审阅中
		ActionContext.getContext().put("exam3",examService.listExamsByCourseAndStatus(courseID,3)); //已批阅试卷
		return "GetInCourse";
	}
	
	
}
